package com.lance.answer.gui.answer.gk.answer;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.DealInfoUtil;
import com.lance.answer.gui.answer.gk.answer.task.DoDaZuoYeTask;
import com.lance.answer.gui.answer.gk.answer.task.UploadFileTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 2022-4-10 上传大作业和行为表现或者社会实践
 */
@Slf4j
public class UploadDaZuoYeAndXingweibiaoxian {

    /**
     * 进度表：
     * 6-18 19:55开始测试 公共行政学 行政管理专科
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        int sync = 15;


        List<String> subjectList = Lists.newArrayList();
        subjectList.add("思想道德修养与法律基础");




        LinkedBlockingDeque<AnswerDTO> answerQueue = new LinkedBlockingDeque<>();

        // 获取所有的用户名和密码
        InputStream userInputStream = new FileInputStream("libs//2022User.txt");
        List<String> userString = IOUtils.readLines(userInputStream, "utf-8");
        Map<String, String> usernameAndPwd = Maps.newHashMap();
        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1].substring(6, 14);
            pwd = "Ouchn" + pwd;
            usernameAndPwd.put(userCode, pwd);
        }

        for(String subjectName:subjectList){
            //  LinkedList<AnswerRequestDTO> requestUrl = Lists.newLinkedList();

            log.info("subjectName:{}",subjectName);
            // 根据课程名称拿到 所有人 这里的用户只有用户名，没有密码
            List<String> userNames = DealInfoUtil.countUserCourseInfo("libs//userCourseInfo").get(subjectName);
            for(String username:userNames){

                if(!Util.getVip().contains(username)){
                    continue;
                }

                String pwd = usernameAndPwd.get(username);
                AnswerDTO answerDTO = new AnswerDTO();
                answerDTO.setSubjectName(subjectName);
                answerDTO.setUsername(username);
                answerDTO.setPwd(pwd);
                answerDTO.setCommit(true);
                answerDTO.setPush(false);
                answerQueue.add(answerDTO);
            }

        }

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new DoDaZuoYeTask(null,answerQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName( "答题线程--" + i);
            answerThread.start();
        }


    }






}
